package CrawlerExtractor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import CrlException.CrlException;
import DataAccessor.DatabaseAccessor;
import java.util.Hashtable;
import java.util.LinkedList;
import org.jsoup.nodes.Document;
import org.jsoup.select.*;

/**
 *
 * @author CUHUUHOANG
 */
public class Extractor implements ExtractorInterface.ExtractorInterface {

    private String url;
    private Document doc;

    @Override
    public String[] getImageLink() throws CrlException {
        try {
            Elements img = doc.select("link");
            LinkedList<String> img_array = new LinkedList<String>();
            for (int t = 0; t < img.size(); t++) {
                if (img.get(t).attr("rel").equals("image_src")) {
                    img_array.add(img.get(t).attr("href"));
                }
            }
            String[] out = new String[img_array.size()];
            for (int i = 0; i < out.length; i++) {
                out[i] = img_array.poll();
            }
            return out;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }
    }

    @Override
    public Hashtable<String, String> getData() throws CrlException {
        try {
            Hashtable<String, String> output = new Hashtable<String, String>();

            String title = "",
                    description = "",
                    keywords = "",
                    reason = "",
                    home = "",
                    website = "",
                    twitter = "";
            Elements meta_info = doc.select("meta");
            String content_tmp, attr_tmp;

            for (int t = 0; t < meta_info.size(); t++) {
                attr_tmp = meta_info.get(t).attr("name");
                content_tmp = meta_info.get(t).attr("content");

                //get title
                if (attr_tmp.equals("title")) {
                    title = content_tmp;
                } //get description
                else if (attr_tmp.equals("description")) {
                    description = content_tmp;
                } //get keyword
                else if (attr_tmp.equals("keywords")) {
                    keywords = content_tmp;
                }
            }
            Elements links = doc.select("a");

            for (int t = 0; t < links.size(); t++) {
                //get home
                if (links.get(t).attr("title").contains("Home")) {
                    home = links.get(t).attr("href");
                } //get website
                else if (links.get(t).attr("title").contains("Website")) {
                    website = links.get(t).attr("href");
                } //get twitter
                else if (links.get(t).attr("title").contains("Twitter")) {
                    twitter = links.get(t).attr("href");
                }
            }
            //get reason
            Elements rs = doc.getAllElements();
            Elements dauvet = doc.getElementsContainingOwnText("Why you should");
            if (dauvet.size() > 0) {
                for (int i = 0; i < rs.size(); i++) {
                    //System.out.println(rs.get(i).toString());
                    if (rs.get(i).toString().equals(dauvet.first().toString())) {
                        reason = rs.get(i + 1).text();
                        break;
                    }
                }
            }
            //put information
            output.put("url", getURL());
            output.put("description", description);
            output.put("title", title);
            output.put("keywords", keywords);
            output.put("reason", reason);
            output.put("home", home);
            output.put("website", website);
            output.put("twitter", twitter);
            return output;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    @Override
    public String[] filterURL(String[] inputURL) throws CrlException {
        try {
            LinkedList<String> url_array = new LinkedList<String>();
            for (int t = 0; t < inputURL.length; t++) {
                if (inputURL[t].contains("speakers")&&(!inputURL[t].contains("#"))) {
                    url_array.add(inputURL[t]);
                }
            }
            String[] out = new String[url_array.size()];
            for (int i = 0; i < out.length; i++) {
                out[i] = url_array.poll();
            }
            return out;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    @Override
    public void setDocument(Document doc) throws CrlException {
        try {
            this.doc = doc;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    @Override
    public void setURL(String url) throws CrlException {
        try {
            this.url = url;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    public String getURL() throws CrlException {
        try {
            return url;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    @Override
    public boolean hasContent() throws CrlException {
        try {
            Elements check_reason = doc.getElementsContainingOwnText("Why you should");
            if (check_reason.size() == 0) {
                return false;
            }
            return true;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    @Override
    public boolean isURLused() throws CrlException {
        try {
            DatabaseAccessor da= new DatabaseAccessor();
            boolean existed=da.valueExisted("url", url);
            //System.out.println(url+":"+existed);
            if(existed)
                return false;
            else return true;
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }
}
